﻿using System;
using System.Windows.Forms;

namespace EasyPath
{
    class EasyPathTabPage : TabPage
    {
        private EasyPathControl _easyPathControl;
        private bool _pathChanged;

        public EasyPathTabPage() : base()
        {
            _easyPathControl = new EasyPathControl
            {
                Dock = DockStyle.Fill
            };

            _easyPathControl.PathChanged += _easyPathControl_PathChanged;
            _easyPathControl.PathSaved += _easyPathControl_PathSaved;

            this.Controls.Add(_easyPathControl);
        }

        public EasyPathControl EasyPathControl
        {
            get { return _easyPathControl; }
        }

        public EnvironmentVariableTarget Target
        {
            get { return _easyPathControl.Target; }
            set { _easyPathControl.Target = value; }
        }

        public override string Text
        {
            get
            {
                return _pathChanged ? base.Text + '*' : base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        public void Populate()
        {
            _easyPathControl.Populate();
        }

        private void RefreshText()
        {
            Text = Text;
        }

        private void _easyPathControl_PathChanged(object sender, EventArgs e)
        {
            _pathChanged = true;

            RefreshText();
        }

        private void _easyPathControl_PathSaved(object sender, EventArgs e)
        {
            _pathChanged = false;

            RefreshText();
        }
    }
}
